<?php

// Include config file
include('./common.php');

// Connect to database
$link = dbConnect();

//start the query to get the forums
$query = "SELECT c.*, f.* FROM ".TABLE_PREFIX."_categories c, ".TABLE_PREFIX."_forums f";

//clean up the guest ID and max days
$guestGroupID = mysql_real_escape_string($_POST['guestGroupID']);
$maxDaysNewThread = time() - 60*60*24*$_POST['maxDaysNewThread'];

// if the user is logged in show the forums they are allowed to see
if(isset($_POST['username'])){
	//get the userID from the login data
	$user = auth($_POST['username'], $_POST['password']);
	if($user == ""){
		echo "output=badLogin";
		return;
	}
	
	//setup a userID for the rest of the script
	$flashUserID = $user['id'];
	
	//get data from the group memebers
	$query .= ", ".TABLE_PREFIX."_groupmembers gm WHERE f.categoryID = c.categoryID";
	
	//add the ability to see the forums they are allowed to as well as guest ones
	$query .= " AND gm.userID = $flashUserID AND f.groupID IN ($guestGroupID, gm.groupID)";
}else{
	//this is a guest, so show them the fourms that only guests can see
	$query .= " WHERE f.groupID = $guestGroupID";
	
	//just get the category ID
	$query .= " AND f.categoryID = c.categoryID";
	
}

//finish off the query
$query .= " GROUP BY f.forumID ORDER BY c.displayOrder, f.displayOrder ASC";

//run the query
$result = mysql_query($query);

if (!$result) {
    fail("mySqlError");
}

//get the number of forums
$forumCount = mysql_num_rows($result);
$output = "&forumCount=$forumCount";

//setup a var to count the number of categories we need to make
$categoryCount = 0;
$currentCategory = -1;

// For each category returned...
for ($i = 0; $i < $forumCount; $i++) {
    $data = mysql_fetch_object($result);
	
	//let's see if the current category is different
	if($data->categoryID != $currentCategory){
		//create the category header
		$output .= "&category" . $categoryCount . "ID=" . $data->categoryID;
		$output .= "&category" . $categoryCount . "title=" . urlencode(stripslashes($data->name));
		$currentCategory = $data->categoryID;
		$categoryCount++;
	}
	
	//build each forum
	$output .= "&forum" . $i . "categoryID=" . $data->categoryID;
	$output .= "&forum" . $i . "forumID=" . $data->forumID;
    $output .= "&forum" . $i . "title=" . urlencode(stripslashes($data->title));
	
	//if we are NOT looking to just get the forum names, then output the goods
	if(!isset($_POST['bareBones'])){		
		//User info
		if($data->lastPost > 0){
			$lastUsername = urlencode(stripslashes(getUsername($data->lastUserID)));
			$lastThreadID = $data->lastThreadID;
			$lastPost = strftime($_POST['categoryDateFormat'], $data->lastPost);
		}else{
			//if there are no posts in the forum
			$lastUsername = 0;
			$lastThreadID = 0;
			$lastPost = 0;
		}

		$output .= "&forum" . $i . "description=" . urlencode(stripslashes($data->description));
		$output .= "&forum" . $i . "postCount=" . number_format($data->postCount);
		$output .= "&forum" . $i . "threadCount=" . number_format($data->threadCount);
		$output .= "&forum" . $i . "lastUsername=" . $lastUsername;
		$output .= "&forum" . $i . "lastThreadID=" . $lastThreadID;
		$output .= "&forum" . $i . "lastPost=" . $lastPost;
		$output .= "&forum" . $i . "forumReadMode=" . $data->readMode;

		//new posts?
		$newPosts = -1;
		if($data->lastPost > $maxDaysNewThread){
			//we've got something new to see
			$newPosts = 0;
			
			//if they are logged in, make sure they also have something new to see
			if(isset($flashUserID)){
				$newPosts = newPostsInForum($flashUserID, $data->forumID, $maxDaysNewThread);
			}
		}
		
		//let flash know
		$output .= "&forum" . $i . "newPosts=".$newPosts;
	}
}

//should have the category count now
$output .= "&categoryCount=".$categoryCount;

//who's online?!?!
$output .= viewOnline($_POST['lastOnlineTimeout'], $_POST['mostOnlineUsers'], $_POST['categoryDateFormat']);

// Output all in one go
echo $output;

// Close link to database server
mysql_close($link);

?>